﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Kugar.Core.BaseStruct;
using Kugar.ERPFramework.BLLInterface;
using Kugar.Core.ExtMethod;
using Kugar.ERPFramework.BLLInterface.Session;

namespace Kugar.ERPFramework.ServiceCommon
{


    public class CustomSession:MarshalByRefObject,ICustomSession
    {
        private Dictionary<string, SessionItem> _cache = new Dictionary<string, SessionItem>(StringComparer.CurrentCultureIgnoreCase);
        private TimerEx _checkerTimer = null;
        private int _expiredTime;

        private static CustomSession _default = new CustomSession();

        public CustomSession()
        {
            _checkerTimer = new TimerEx(checkSessionExpired, 20000, null);
            _expiredTime = 300000;

            _checkerTimer.IsStopWhenRun = true;
            _checkerTimer.Start();
        }



        /// <summary>
        ///     过期时间,,以秒为单位,默认为5分钟(300秒)
        /// </summary>
        public int ExpiredTime
        {
            get { return _expiredTime; }
            set { _expiredTime = value; }
        }

        public void ActiveSession(string sessionID)
        {
            var session = getSessionByID(sessionID);

            session.Active();

        }

        public void Clear()
        {
            _cache.Clear();
        }

        public static CustomSession Default
        {
            get { return _default; }
        }


        private SessionItem getSessionByID(string sessionID)
        {
            if (_cache.ContainsKey(sessionID))
            {
                return _cache[sessionID];
            }
            else
            {
                var item = new SessionItem();

                item.Active();

                item.SessionID = sessionID;

                _cache.Add(sessionID, item);

                return item;
            }
        }

        private void checkSessionExpired(object state)
        {
            if (_cache.Count <= 0)
            {
                return;
            }

            var expiredDt = DateTime.Now.AddSeconds(-1 * _expiredTime);

            _cache.Remove(x => x.Value.LastActiveTime < expiredDt);
        }

        #region ICustomSession 成员

        public SessionItem this[string sessionID]
        {
            get
            {
                return getSessionByID(sessionID);
            }
        }

        public SessionItem CurrentSession
        {
            get 
            {
                return getSessionByID(CurrentSessionID);
            }
        }

        public string CurrentSessionID { get { return OperationContext.Current.SessionId; } }

        public void Remvoe(string sessionID)
        {
            _cache.Remove(sessionID);
        }

        #endregion

        #region IEnumerable<SessionItem> 成员

        public IEnumerator<SessionItem> GetEnumerator()
        {
            foreach (var item in _cache)
            {
                yield return item.Value;
            }
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }


}
